<template>
  <el-dialog v-model="visible" title="查看内容" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-descriptions class="margin-top" :column="2" border>
      <el-descriptions-item align="center">
        <template #label>
          <div class="cell-item">项目名称</div>
        </template>
        {{ dataForm.name }}
      </el-descriptions-item>
      <el-descriptions-item align="center">
        <template #label>
          <div class="cell-item">项目简介</div>
        </template>
        {{ dataForm.info }}
      </el-descriptions-item>
    </el-descriptions>
    <div class="btns" style="margin: 20px auto">
      <el-button type="primary" plain @click="checkPage(dataForm.id)">点击查看论文</el-button>
      <el-button type="primary" plain @click="checkCode(dataForm.id)">点击查看源代码</el-button>
      <el-button type="primary" plain @click="checkDataSet(dataForm.id)">点击查看数据集</el-button>
    </div>
    <template v-slot:footer>
      <el-button v-if="props.isAudit" type="primary" @click="auditVisible = true">点击进行审核</el-button>
      <el-button @click="visible = false">{{ props.isAudit ? "取消" : "关闭" }}</el-button>
    </template>
  </el-dialog>
  <Page ref="PageRef" @refreshDataList="state.getDataList" :projectId="dialogData.currentId"
    :projectStatus="state.currentStatus" @getPageId="getPageId"></Page>
  <Code ref="CodeRef" @refreshDataList="state.getDataList" :projectId="dialogData.currentId"
    :projectStatus="state.currentStatus" @getCodeId="getCodeId"></Code>
  <DataSet ref="DataSetRef" @refreshDataList="state.getDataList" :projectId="dialogData.currentId"
    :projectStatus="state.currentStatus" @getDataSetId="getDataSetId"></DataSet>

  <el-dialog v-model="auditVisible" title="审核" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-radio-group v-model="state.result" class="ml-4">
      <el-radio label="3" size="large">通过</el-radio>
      <el-radio label="2" size="large">重新修订</el-radio>
    </el-radio-group>
    <template v-slot:footer>
      <el-button @click="auditVisible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="auditHandle(dataForm.id)">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { nextTick, reactive, ref, toRefs } from "vue";
import baseService from "@/service/baseService";
import WangEditor from "@/components/wang-editor/index.vue";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
import Page from "@/views/poap/checkPage.vue";
import Code from "@/views/poap/checkCode.vue";
import DataSet from "@/views/poap/checkDataset.vue";
import useView from "@/hooks/useView";

const { t } = useI18n();

const emit = defineEmits(["refreshDataList", "getDataListChild"]);

const visible = ref(false);
const licenseVisible = ref(false);
const auditVisible = ref(false);
const dataFormRef = ref();

const props = defineProps({
  projectId: String,
  isAudit: Boolean
});

const dataForm = reactive({
  id: "",
  name: "",
  info: "",
  status: "",
  auditorId: "",
  viewNumber: "",
  downloadNumber: "",
  codeStatus: 0,
  codeActive: true,
  pageStatus: 0,
  pageActive: true,
  datasetStatus: 0,
  datasetActive: true,
  licenseId: "",
  licenseName: "",
  delFlag: "",
  createDate: "",
  creator: "",
  updateDate: "",
  updater: ""
});

const dialogData = reactive({
  currentId: "", //当前项目id
  currentStatus: 0, //当前项目状态
  drawerPage: false, //论文抽屉
  drawerCode: false, //源代码抽屉
  drawerDataSet: false //数据集抽屉
});

const view = reactive({
  getDataListURL: "/poap/poapopensourcelicense/page",
  getDataListIsPage: true,
  exportURL: "/poap/poapopensourcelicense/export",
  deleteURL: "/poap/poapopensourcelicense",
  deleteIsBatch: true,
  dataForm: {
    name: ""
  },
  currentStatus: 0,
  auditorId: "",
  type: [],
  label: [],
  projectLabel: [],
  projectType: [],
  pageId: [],
  codeId: [],
  dataSetId: [],
  result: "",
  participantIds: [] as any[],
  projectName: "",
  isAudit: false
});

const state = reactive({ ...useView(view), ...toRefs(view) });
const sysNotice = reactive({
  id: "",
  noticeType: 2,
  title: "项目通知",
  content: "",
  receiverType: 0,
  receiverTypeIds: "",
  receiverTypeList: [],
  status: 1,
  senderName: "系统发送",
  senderDate: "",
  creator: 0,
  createDate: ""
});

const auditHandle = (id: string) => {
  console.log(dataForm.id);
  if (state.result === "2") {
    baseService
      .put("/poap/poapproject/projectNoPass", {
        projectId: id
      })
      .then((res) => {
        console.log(res);
        if (res.code != 0) return false;

        console.log(state.auditorId);
        baseService.put("/poap/poapproject/updateAuditor", { projectId: id, auditorId: state.auditorId }).then((res) => {
          console.log(res);
          if (res.code != 0) return false;
        });
        sysNotice.content = `您参与的项目《${state.projectName}》状态已改变，请及时查看`;
        console.log(sysNotice);
        console.log(state.participantIds);
        baseService.post("/poap/poapparticipantproject/notice", { noticeDTO: sysNotice, projectId: id }).then((res) => {
          console.log(res);
          if (res.code != 0) return false;
        });
        emit("getDataListChild");
        auditVisible.value = false;
        visible.value = false;
        ElMessage.success("审核成功");
      });
  } else if (state.result === "3") {
    baseService
      .put("/poap/poapproject/projectPass", {
        projectId: id
      })
      .then((res) => {
        console.log(res);
        if (res.code != 0) return false;
        console.log(state.auditorId);
        baseService.put("/poap/poapproject/updateAuditor", { projectId: id, auditorId: state.auditorId }).then((res) => {
          console.log(res);
          if (res.code != 0) return false;
        });
        sysNotice.content = `您参与的项目《${state.projectName}》状态已改变，请及时查看`;
        console.log(sysNotice);
        console.log(state.participantIds);
        baseService.post("/poap/poapparticipantproject/notice", { noticeDTO: sysNotice, projectId: id }).then((res) => {
          console.log(res);
          if (res.code != 0) return false;
        });
        emit("getDataListChild");
        auditVisible.value = false;
        visible.value = false;
        ElMessage.success("审核成功");
      });
  }
};

const rules = ref({});

const init = (userId: string, id?: string) => {
  state.participantIds[0] = userId;
  state.auditorId = userId;
  state.isAudit = props.isAudit;
  visible.value = true;
  // if (id) {
  //   dataForm.id=id
  //   let idn=parseInt(id)
  //   getInfo(idn);
  // }
  baseService.get("/poap/poapproject/" + id).then((res) => {
    console.log(res);
    Object.assign(dataForm, res.data);
  });
  baseService.get("/poap/poapparticipantproject/selectProjectName", { projectId: id }).then((res) => {
    console.log(res);
    if (res.code != 0) return false;
    state.projectName = res.data;
  });

  console.log(dataForm);
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/poap/poapproject/" + id).then((res) => {
    console.log(res);
    Object.assign(dataForm, res.data);
  });
};

const PageRef = ref();
const checkPage = (id: string) => {
  dialogData.drawerPage = true;
  dialogData.currentId = "";
  dialogData.currentId = id;
  nextTick(() => {
    PageRef.value.initPage();
    PageRef.value.$forceUpdate();
  });
};
const getPageId = (val: any) => {
  state.pageId = val;
};

const CodeRef = ref();
const checkCode = (id: string) => {
  dialogData.drawerCode = true;
  dialogData.currentId = "";
  dialogData.currentId = id;
  nextTick(() => {
    CodeRef.value.initCode();
    CodeRef.value.$forceUpdate();
  });
};
const getCodeId = (val: any) => {
  state.codeId = val;
};

const DataSetRef = ref();
const checkDataSet = (id: string) => {
  dialogData.drawerDataSet = true;
  dialogData.currentId = "";
  dialogData.currentId = id;
  nextTick(() => {
    DataSetRef.value.initDataSet();
    DataSetRef.value.$forceUpdate();
  });
};
const getDataSetId = (val: any) => {
  state.dataSetId = val;
};

defineExpose({
  init
});
</script>
